package com.neusoft.elm.controller;

import com.neusoft.elm.po.Cart;
import com.neusoft.elm.service.CartService;
import com.neusoft.elm.service.CartServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class CartController {
    CartService cartService=new CartServiceImpl();
    public Object listCart(HttpServletRequest request)throws  Exception{
        String businessIdStr = request.getParameter("businessId");
        Integer businessId=null;
        if(businessIdStr!=null){
            businessId=Integer.valueOf(businessIdStr);
        }
        String userId=request.getParameter("userId");
        Cart cart=new Cart();
        cart.setBusinessId(businessId);
        cart.setUserId(userId);
        List<Cart> list = cartService.listCart(cart);
        return list;
    }

    public Object saveCart(HttpServletRequest request)throws  Exception{
        Integer businessId=Integer.valueOf(request.getParameter("businessId"));
        String userId = request.getParameter("userId");
        Integer foodId=Integer.valueOf(request.getParameter("foodId"));
        Cart cart=new Cart();
        cart.setBusinessId(businessId);
        cart.setUserId(userId);
        cart.setFoodId(foodId);
        int n = cartService.saveCart(cart);
        return n;
    }
    public Object removeCart(HttpServletRequest request)throws Exception{
        Integer businessId=Integer.valueOf(request.getParameter("businessId"));
        String userId = request.getParameter("userId");
        Integer foodId=Integer.valueOf(request.getParameter("foodId"));
        Cart cart=new Cart();
        cart.setBusinessId(businessId);
        cart.setUserId(userId);
        cart.setFoodId(foodId);
        int n = cartService.removeCart(cart);
        return n;
    }
    public Object updateCart(HttpServletRequest request)throws Exception{
        Integer businessId=Integer.valueOf(request.getParameter("businessId"));
        String userId = request.getParameter("userId");
        Integer foodId=Integer.valueOf(request.getParameter("foodId"));
        Integer quantity=Integer.valueOf(request.getParameter("quantity"));
        Cart cart=new Cart();
        cart.setBusinessId(businessId);
        cart.setUserId(userId);
        cart.setFoodId(foodId);
        cart.setQuantity(quantity);
        int n = cartService.updateCart(cart);
        return n;
    }
}

